import * as service from '../../services/execService';
import { EXECTYPE } from '../../utils/types';
export default {
  namespace: 'execMgr',
  state: {
    execType: '',
    list: {
      queryStr: '',
      datas: [],
      pageSize: 10,
      currentPage: 1,
      totalCount: 0
    }
  },
  reducers: {
    updateList(state, { payload: { queryStr, datas, currentPage, totalCount, execType } }) {
      return { ...state, execType: execType, list: { ...state.list, queryStr: queryStr, datas: datas, currentPage: currentPage, totalCount: totalCount } };
    }
  },
  effects: {
    *create({ payload: values }, { call }) {
      const res = yield call(service.create, values);
      if (res != null && res.success) {

      } else {
        console.warn('execMgr_create_error' + res);
      }
    },
    *update({ payload: values }, { call }) {
      const res = yield call(service.update, values);
      if (res != null && res.success) {

      } else {
        console.warn('execMgr_update_error' + res);
      }
    },
    *delete({ payload: values }, { call }) {
      const res = yield call(service.delete, values);
      if (res != null && res.success) {

      } else {
        console.warn('execMgr_delete_error' + res);
      }
    },
    *initConfig({ }, { call }) {
      yield call(service.initConfig);
    },
    *listTurnToPage({ payload: { execType, currentPage, pageSize, queryStr } }, { select, call, put }) {
      if (queryStr === undefined) {
        queryStr = yield select(state => state.execMgr.list.queryStr);
      }
      if (currentPage === undefined || currentPage == null) {
        currentPage = yield select(state => state.execMgr.list.currentPage);
      }
      if (pageSize === undefined || pageSize == null) {
        pageSize = yield select(state => state.execMgr.list.pageSize);
      }
      console.warn(currentPage);
      const res = yield call(service.getList, { execType: execType, currentPage: currentPage, pageSize: pageSize, queryStr: queryStr });
      if (res === undefined || res.result == null) {
        return;
      }
      else {
        yield call(service.initConfig, { execType: execType });
        yield put(
          {
            type: 'updateList',
            payload: {
              queryStr: queryStr,
              datas: res.result.items,
              currentPage: currentPage,
              totalCount: res.result.totalCount,
              execType: execType
            }
          }
        )
      }
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      dispatch({
        type: 'initConfig'
      });
      history.listen(({ pathname }) => {
      })
    }
  },
};
